package com.chenxi.service.impl;

import com.chenxi.VO.ResourceMenuVO;
import com.chenxi.common.service.BaseServiceImpl;
import com.chenxi.constant.Constant;
import com.chenxi.mapper.SysResourceMapper;
import com.chenxi.mapper.SysUserMapper;
import com.chenxi.model.SysResource;
import com.chenxi.service.ISysResourceService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author Hu Xiaolong
 * @version V0.1
 * @create 2017/12/20
 */
@Service("sysResourceService")
public class SysResourceServiceImpl extends BaseServiceImpl<SysResource> implements ISysResourceService{

    // 将mapper注入到BaseMapper中
    @Autowired
    public SysResourceServiceImpl(SysResourceMapper resourceMapper) {
        super(resourceMapper);
    }
    /**
     *
     * @return
     */
    @Override
    public Constant.ResourceType[] getResourceType(){
        return Constant.ResourceType.values();
    }

    @Override
    public List<SysResource> getResourceMenusByUser(Long userId) {
        return null;
    }

    @Override
    public List<ResourceMenuVO> loadMenuResources(Long userId) {
        Example example = new Example(SysResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resourceType","M");
        return this.grentMenus(super.getBaseMapper().selectByExample(example));
    }
    private List<ResourceMenuVO> grentMenus(List<SysResource> resources){
        List<ResourceMenuVO> menus = Lists.newArrayList();
        resources.forEach(resource -> {
            if (resource.getParentId() == 0L){
                ResourceMenuVO m = new ResourceMenuVO();
                m.setId(resource.getId());
                m.setText(resource.getResourceName());
                m.setLink(resource.getResourcePath());
                m.setIcon(resource.getResourceIcon());
                m.setHeading(false);
                m.setTarget("");
                menus.add(m);
            }
        });
        menus.forEach(menu -> {
            List<ResourceMenuVO> subMenus = Lists.newArrayList();
            resources.forEach(resource->{
                if (resource.getParentId()==menu.getId()){
                    ResourceMenuVO m = new ResourceMenuVO();
                    m.setText(resource.getResourceName());
                    m.setLink(resource.getResourcePath());
                    m.setIcon(resource.getResourceIcon());
                    m.setHeading(false);
                    m.setTarget("");
                    subMenus.add(m);
                }
            });
            menu.setSubmenu(subMenus);
        });
        return menus;
    }

}

